#!/usr/bin/perl -w # for syntax highlighting
# CSS-V1

use gri_secmgr qw(get_cookie_value got_cookie redirect_header);
use gri_database;
use Data::Dumper;
use MIME::Base64;

# 
# Show all available sections that can be deleted or modified.
# We use the action to set the appropriate link...
#

use Data::Dumper;

sub mm_form_docrepos_delete_do($) {
my $section=shift;

	my $section2=hex_to_string($section);

	my $db_dir=$::GRI_FRONTEND->get_global_directory("database");
	my $docrepos=$::GRI_FRONTEND->get_global_directory("docrepos");
	my $DB=new gri_database("$db_dir/gri.db");

	# We firstly find documents that are only in this section...

	my ($a,$b)=$DB->get_doclist_unique_to_section(section => $section2);
	if($a!=1) {
		print "Content-type: text/html\n\n";
		print "<font color=red>$b</font><br>\n";
		print $r;
		exit(0);
	}
	for my $cdoc (@$b) {
		my ($sha1,$lname)=split(/\|/,$cdoc);
		my ($c,$d);
		($c,$d)=$DB->remove_doc(sha1=>$sha1,lname=>$lname,history=>1);
		if($c!=1) {
			print STDERR "Error deleting $sha1/$lname : $d\n";
		}

		# delete file, or strings from it when we find it...
		opendir($dfd,$docrepos);
		if($dfd) {
			my @M=grep {/^$lname.*/} readdir($dfd);
			closedir($dfd);
			for my $cmat (@M) {
				if(-f "$docrepos/$cmat") {
					unlink("$docrepos/$cmat");
				}
			}
		}
	}

	# Now that all unique documents have been removed we now need 
	# to remove the section definition too...

	$DB->remove_section(section => $section2);

	# After doing this we re-direct to the admin page...
	my $link=link_to_str("","","/admin_page","");
	redirect_header(path => $link);
	exit(0);
}

# vim: ts=4:sw=4
